package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.CommentsActivity;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.modles.BaskDetailsData;

import java.util.List;

/**
 * 晒单详情页面 的评论 的适配器
 * Created by lanouhn on 16/9/12.
 */
public class BaskCommentAdapter extends RecyclerView.Adapter<BaskCommentAdapter.BaskCommentViewHolder>{

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private Context context;
    private List<BaskDetailsData.DataBean.PostBean.CommentsBean> commentsBeen;
    private String comments_ID;

    public BaskCommentAdapter(Context context, List<BaskDetailsData.DataBean.PostBean.CommentsBean> commentsBeen,String comments_ID) {
        this.context = context;
        this.commentsBeen = commentsBeen;
        this.comments_ID=comments_ID;
    }

    @Override
    public BaskCommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.act_sife_second_three_item_review,null);
        BaskCommentViewHolder holder=new BaskCommentViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        return holder;
    }

    @Override
    public void onBindViewHolder(BaskCommentViewHolder holder, final int position) {
            holder.ltView.setVisibility(View.VISIBLE);
            holder.ltTwo.setVisibility(View.VISIBLE);
            holder.author.setText(commentsBeen.get(position).getNickname()+"：");
            holder.rwContent.setText(commentsBeen.get(position).getConent());

        //跳转至 评论页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, CommentsActivity.class);
                intent.putExtra("comments_iD", comments_ID);
                context.startActivity(intent);
            }
        });

        //跳转至 作者介绍页面
        holder.author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID =commentsBeen.get(position).getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return commentsBeen.size();
    }

    public class BaskCommentViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout ltView, ltTwo;
        private TextView  author, rwContent;

        public BaskCommentViewHolder(View itemView) {
            super(itemView);

            ltView = (LinearLayout) itemView.findViewById(R.id.lt_view);//主容器，控制 有人评论时显示。
            ltTwo = (LinearLayout) itemView.findViewById(R.id.ltTwo_review_three_item_review);//容器，控制显示评论
            author = (TextView) itemView.findViewById(R.id.author_three_item_review);//作者
            rwContent = (TextView) itemView.findViewById(R.id.rwContent_three_item_review);//评论内容

        }
    }
}
